package com.lwq.cocos;

import java.util.HashMap;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lwq.util.Constance;
import com.lwq.util.FileUtil;
import com.lwq.util.GsonUtil;
import com.lwq.view.LeftEditorView;

public class ProjectInfoAdpter {
	private static ProjectInfoAdpter instance = null;
	private ProjectInfo mProInfo = null;
	private Map<DefaultMutableTreeNode, CCNode> NodesMap = null;
	private Map<CCNode, CCNode> ParentsMap = null;
	private CCNode mSelectNode = null;
	private DefaultMutableTreeNode mSelectTreeNode = null;

	public static ProjectInfoAdpter getInstance() {
		if (instance == null) {
			instance = new ProjectInfoAdpter();
		}
		return instance;
	}

	public void saveProInfo(String path) {
		GsonUtil.getInstance(GsonUtil.BUILD_TYPE).modelToFile(
				ProjectInfoAdpter.getInstance().getProInfo(), path);
	}

	public CCNode getSelectNode() {
		return mSelectNode;
	}
	public DefaultMutableTreeNode getSelectTreeNode() {
		return mSelectTreeNode;
	}

	public void setSelectNode(DefaultMutableTreeNode treeNode) {
		mSelectTreeNode = treeNode;
		this.mSelectNode = NodesMap.get(treeNode);
	}

	public void nodesMapAddItem(DefaultMutableTreeNode treeNode, CCNode node) {
		NodesMap.put(treeNode, node);
		if (node.getChilds() != null) {
			for (int i = 0; i < node.getChilds().size(); i++) {
				ParentsMap.put(node.getChilds().get(i), node);
			}
		}
	}

	public void setRoot(DefaultMutableTreeNode treeNode) {
		mSelectTreeNode = treeNode;
		mSelectNode = mProInfo;
		NodesMap.put(treeNode, mProInfo);
	}

	public ProjectInfoAdpter() {
		NodesMap = new HashMap<DefaultMutableTreeNode, CCNode>();
		ParentsMap = new HashMap<CCNode, CCNode>();
	}

	public ProjectInfo getProInfo() {
		return mProInfo;
	}

	public void setCCNodeJson(JSONObject json, CCNode selModel) {
		JSONArray childJsonList = null;
		JSONObject childJson = null;
		CCNode childNode = null;
		CCNode newNode = null;
		int type = 0;
		if (json != null) {
			try {
				childJsonList = json.getJSONArray(Constance.KEY_STR_CHILDS);
				if (childJsonList != null) {
					for (int i = 0; i < childJsonList.length(); i++) {
						childNode = selModel.getChilds().get(i);
						type = childNode.getType();
						if (type != Constance.NODE_TYPE) {
							childJson = childJsonList.getJSONObject(i);
							newNode = (CCNode) GsonUtil.getInstance(
									GsonUtil.BUILD_TYPE).jsonToModel(
									childJson.toString(),
									Constance.getCCClass(type));
							selModel.getChilds().remove(childNode);
							selModel.getChilds().add(i, newNode);
							setCCNodeJson(childJson, newNode);
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public void newProInfo(String pType) {
		mProInfo = new ProjectInfo(pType);
	}

	public void decodeProInfo(String path) {
		JSONObject json = null;
		String jsonData = (String) FileUtil.readFileData(path, 0,
				FileUtil.FILEEND, FileUtil.DATATYPE_STRING);
		if (jsonData == null)
			return;
		try {
			json = new JSONObject(jsonData);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mProInfo = (ProjectInfo) GsonUtil.getInstance(GsonUtil.BUILD_TYPE)
				.jsonToModel(jsonData, ProjectInfo.class);
		setCCNodeJson(json, mProInfo);
		mProInfo.init();
	}

	public Map<DefaultMutableTreeNode, CCNode> getNodesMap() {
		return NodesMap;
	}

	public CCNode getParentNode(CCNode node) {
		if (ParentsMap != null) {
			if (ParentsMap.containsKey(node)) {
				return ParentsMap.get(node);
			}
		}
		return null;
	}

	public void removeNode() {
		if (mSelectTreeNode != null && mSelectNode != null) {
			NodesMap.remove(mSelectTreeNode);
			LeftEditorView.getInstance().removeNode(mSelectTreeNode);
			if (ParentsMap.containsKey(mSelectNode)) {
				ParentsMap.get(mSelectNode).RemoveChild(mSelectNode);
			}
			mSelectTreeNode = null;
			mSelectNode = null;
			LeftEditorView.getInstance().TreeReLoadData();
			LeftEditorView.getInstance().refreshScreen();
		}
	}

	public void addNode(CCNode node) {
		DefaultMutableTreeNode childTreeNode = null;
		String treeNodeName = null;
		if (mSelectTreeNode != null && mSelectNode != null) {
			mSelectNode.addChild(node);
			treeNodeName = node.getTreeName();
			childTreeNode = new DefaultMutableTreeNode(treeNodeName);
			mSelectTreeNode.add(childTreeNode);
			LeftEditorView.getInstance().TreeReLoadData();
			LeftEditorView.getInstance().refreshScreen();
			NodesMap.put(childTreeNode, node);
		}
	}

}
